package com.flux.tms.tm${bean.numMain}srv.common.ctrl;

import static org.mockito.ArgumentMatchers.any;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.BeforeClass;

import com.flux.wmsv5.comcomp.OnsProducer;
import com.flux.wmsv5.comobjs.CommonCVal;
import com.flux.wmsv5.comobjs.LoginInfoObj;
import com.flux.wmsv5.comobjs.ResultObj;
import com.flux.wmsv5.comobjs.RowDataException;
import com.flux.wmsv5.comservice.AppSecretService;
import com.flux.wmsv5.comservice.DefaultBsmConfigServiceImpl;
import com.flux.wmsv5.comservice.SysConfigService;
import com.flux.wmsv5.comservice.api.BsmConfigService;
import com.flux.wmsv5.comservice.api.IdSeqService;
import com.flux.wmsv5.comservice.api.StdBizExtExecuteService;
import com.flux.wmsv5.utils.BizDataUtil;
import com.flux.wmsv5.utils.LoginInfoUtil;
import com.flux.wmsv5.utils.RequestIdUtil;

/**
 * 控制公共类
 * 
 * <p>
 * 创建时间：${bean.dayTime}
 * <p>
 * 
 * <p>
 * 修改时间：${bean.dayTime}
 * <p>
 * 
 * @author ${bean.author}
 * 
 * @version V1.0.0
 *
 */
public abstract class CommonCtrl {
	@Mock
	protected AppSecretService appSecretService;
	@Mock
	protected OnsProducer onsProducer;
	@Mock
	protected StdBizExtExecuteService stdBizExtExecuteService;
	@Mock
	protected IdSeqService idSeqService;

	protected ResultObj reObj = new ResultObj();
	protected ResultObj reObjErr = new ResultObj();
	protected LoginInfoObj loginInfoObj = new LoginInfoObj();
	protected Map<String,String> bizDataMap = new HashMap<String, String>();
	protected Map<String, Object> tmpMap = new HashMap<String, Object>();
	protected List<Map<String, Object>> tmpMapList = new ArrayList<Map<String,Object>>();
	protected String bizData;
	protected BsmConfigService bsmConfigService = new DefaultBsmConfigServiceImpl();
	protected RowDataException RowDataException = new RowDataException("A","mes");
	protected Exception exception = new Exception("A");

	@Mock
	protected SysConfigService sysConfigService;


	 @BeforeClass
	 public void beforeClass() {
		 CommonCtrl commonCtrl = returnSubClass();
		 MockitoAnnotations.initMocks(commonCtrl);
	 }	 
	 
	 /**
	  * 返回自己对象
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  * 
	  * @return
	  */
	 protected abstract CommonCtrl returnSubClass();
	 
	 /**
	  * 初始化登录对象
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 protected void initLogin(){
		 loginInfoObj.setUserId("DEVTEST");
		 loginInfoObj.setUserName("DEVTEST");
		 loginInfoObj.setRemoteClientAddr("0.0.0.0");
		 loginInfoObj.setRequestId("DEVTEST");
		 loginInfoObj.setBizWarehouseId("TEST100");
		 loginInfoObj.setBizOrgId("FLUX");
		 loginInfoObj.setRequestId(RequestIdUtil.createId("A2002"));
		 initSubLogin();
	 }
	 
	 /**
	  * 子类初始化登录对象
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 protected abstract void initSubLogin();
	 
	 /**
	  * 初始化业务数据信息
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 protected void initBizDataMap(){
		 bizDataMap.put("organizationId", "FLUX");
		 bizDataMap.put("warehouseId", "*");
		 initSubBizDataMap();
	 }
	 
	 /**
	  * 子类初始化业务数据信息
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 protected abstract void initSubBizDataMap();
	 
	 /**
	  * 测试login对象没有数据
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 public void loginNoData(){
		//测试login对象没有数据
		PowerMockito.doReturn("ABC").when(appSecretService).getAppSecret();
		String appSecret = appSecretService.getAppSecret();
		String loginInfo = LoginInfoUtil.toJSONString(loginInfoObj, appSecret);
		String bizData = BizDataUtil.toJSONString(bizDataMap, appSecret);
		loginNoDataSub(loginInfo, bizData);
	 }
	 
	 protected abstract void loginNoDataSub(String loginInfo, String bizData);
	 
	 /**
	  * 测试login对象没有数据
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 public void bizDataNoData(){
		//测试login对象没有数据
		PowerMockito.doReturn("ABC").when(appSecretService).getAppSecret();
		String appSecret = appSecretService.getAppSecret();
		String loginInfo = LoginInfoUtil.toJSONString(loginInfoObj, appSecret);
		String bizData = BizDataUtil.toJSONString(bizDataMap, appSecret);
		bizDataNoDataSub(loginInfo, bizData);
	 }
	 
	 protected abstract void bizDataNoDataSub(String loginInfo, String bizData);
	 
	 /**
	  * 测试除正常外其他逻辑
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  *
	  */
	 public void testOther() throws Exception{
		//测试login对象没有数据
		PowerMockito.doReturn("ABC").when(appSecretService).getAppSecret();
		String appSecret = appSecretService.getAppSecret();
		String loginInfo = LoginInfoUtil.toJSONString(loginInfoObj, appSecret);
		String bizData = BizDataUtil.toJSONString(bizDataMap, appSecret);
		testOtherSub(loginInfo, bizData);
	 }
	 protected abstract void testOtherSub(String loginInfo, String bizData) throws Exception;
	 
	 /**
	  * 测试正常
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	 * @throws Exception 
	  *
	  */
	 public void testOK() throws Exception{
		//测试login对象没有数据
		PowerMockito.doReturn("ABC").when(appSecretService).getAppSecret();
		String appSecret = appSecretService.getAppSecret();
		String loginInfo = LoginInfoUtil.toJSONString(loginInfoObj, appSecret);
		String bizData = BizDataUtil.toJSONString(bizDataMap, appSecret);
		testOKSub(loginInfo, bizData);
	 }
	 protected abstract void testOKSub(String loginInfo, String bizData) throws Exception;
	 
	 /**
	  * 从新获取业务数据
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  * 
	  * @return
	  */
	 public String bizDataMapToString(){
		PowerMockito.doReturn("ABC").when(appSecretService).getAppSecret();
		String appSecret = appSecretService.getAppSecret();
		return BizDataUtil.toJSONString(bizDataMap, appSecret);
	 }
	 public String loginObjToString(){
	        PowerMockito.doReturn("ABC").when(appSecretService).getAppSecret();
	        String appSecret = appSecretService.getAppSecret();
	        return LoginInfoUtil.toJSONString(loginInfoObj, appSecret);
     }
	 
	 /**
	  * 对于一些公共的需要初始化
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  * 
	  * @throws Exception
	  */
	 @SuppressWarnings("unchecked")
	public void init() throws Exception{
		 reObj.setCodeValueOK();      
		 PowerMockito.when(stdBizExtExecuteService.execute(any(LoginInfoObj.class),any(Map.class),any(String.class),any(String.class),any(String.class))).thenReturn(reObj);
		 reObj.setPkey01("111");
		 PowerMockito.when(idSeqService.getNextId(any(LoginInfoObj.class),any(Map.class))).thenReturn(reObj);
	 }
	 
	 /**
	  * 子类需要实现的初始化
	  * 
	  * <p>Author: ${bean.author}
	  * <p>Create Time: ${bean.dayTime}
	  * 
	  * @throws Exception
	  */
	 protected abstract void initSub() throws Exception;
}
